<template>
  <el-dialog
    :title="isWorking ? $t('user.addWorker') : $t('user.userImport')"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @close="onCancel"
    width="38%"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="昵称" prop="nickName">
        <el-input
          size="small"
          class="value-width"
          v-model="form.nickName"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          size="small"
          maxlength="11"
          class="value-width"
          v-model="form.phone"
        ></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group size="small" v-model="form.sex">
          <el-radio label="0">男</el-radio>
          <el-radio label="1">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="余额" prop="balanceValue">
        <el-input-number
          v-model="form.balanceValue"
          controls-position="right"
          :min="0"
          size="small"
          :max="100000000"
        ></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="onCancel">取 消</el-button>
      <el-button type="primary" @click="onConfirm">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
  export default {
    props: {
      isWorking: {
        type: Boolean,
        default: false,
      },
    },
    data() {
      return {
        visible: false,
        rules: {
          nickName: [{ required: true, message: "请输入昵称", trigger: "blur" }],
          phone: [
            { required: true, message: "请输入手机号", trigger: "blur" },
            {
              pattern: /^1[3456789]\d{9}$/,
              message: "请输入正确的手机号",
              trigger: "blur",
            },
          ],
          sex: [{ required: true, message: "请选择性别", trigger: "change" }],
        },
        form: {
          //           电话 phone
          // 密码 password
          // 昵称 nickName
          // 余额 balanceValue
          // 性别 sex
          phone: "",
          password: "",
          nickName: "",
          balanceValue: 0,
          sex: "",
        },
      };
    },
    methods: {
      open() {
        this.visible = true;
      },
      onConfirm() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl("/user/userTag/addUserTag"),
              method: "post",
              data: this.$http.adornData({
                ...this.form,
              }),
            }).then((res) => {
              console.log(res);
              if (res.code == "00000") {
                this.$message({
                  message: "操作成功",
                  type: "success",
                });
                this.$emit("refresh");
              }
              this.onCancel();
            });
          }
        });
      },
      onCancel() {
        this.form = {
          phone: "",
          password: "",
          nickName: "",
          balanceValue: 0,
          sex: "",
        };
        this.$refs.form.resetFields();
        this.visible = false;
      },
    },
  };
</script>
